package com.woniuxy.controller;

import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.TestOptions;
import com.woniuxy.service.TestOptionsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TestOptions)表控制层
 *
 * @author zjx
 * @since 2021-05-14 19:42:03
 */
@RestController
@RequestMapping("/testOptions")
public class TestOptionsController {
    /**
     * 服务对象
     */
    @Resource
    private TestOptionsService testOptionsService;

    /**
     * Description : 通id过试题id查询所有试题选项
     * ChangeLog : 1. 创建 (2021/5/17 10:52 [zjx]);
     * @param testId  试题i     * @return null
    */
    @RequestMapping("selectByTestId")
    public ResponseEntity selectByTestId(int testId){
        List<TestOptions> testOptions = this.testOptionsService.selectByTestId(testId);
        return new ResponseEntity(200,"success",testOptions);
    }
    /**
     * Description : 以试题id新增多行数据
     * ChangeLog : 1. 创建 (2021/5/17 10:58 [zjx]);
     * @param list  封装实体类的集合
     * @return null
    */
    @RequestMapping("insertBatch")
    public ResponseEntity insertBatch(List<TestOptions> list){
        int row = this.testOptionsService.insertBatch(list);
        return new ResponseEntity(200,"success",row);
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "/selectOne",produces = {"application/json;charset=utf-8"})
    public ResponseEntity selectOne(Integer id) {
        ResponseEntity resultMsg = new ResponseEntity(200,"查询失败",null);
        TestOptions testOptions = this.testOptionsService.queryById(id);
        if (testOptions!=null) {
            resultMsg.setCode(200);
            resultMsg.setData(testOptions);
            resultMsg.setMessage("查询成功!");
        }
        return resultMsg;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @GetMapping(value = "/selectAllByLimit",produces = {"application/json;charset=utf-8"})
    public ResponseEntity selectAllByLimit(int offset, int limit) {
        ResponseEntity resultMsg = new ResponseEntity(401,"查询失败",null);
        List<TestOptions> testOptionsList = this.testOptionsService.queryAllByLimit(offset, limit);
        if (testOptionsList!=null) {
            resultMsg.setCode(200);
            resultMsg.setData(testOptionsList);
            resultMsg.setMessage("查询成功!");
        }
        return resultMsg;
    }
    /**
     * 新增数据
     *
     * @param testOptions 实例对象
     * @return 实例对象
     */
    @PostMapping(value = "/insert",produces = {"application/json;charset=utf-8"})
    public ResponseEntity insert(TestOptions testOptions) {
        ResponseEntity resultMsg = new ResponseEntity(401,"新增失败",null);
        int row = this.testOptionsService.insert(testOptions);
        if (row!=0) {
            resultMsg.setCode(200);
            resultMsg.setMessage("新增成功!");
        }
        return resultMsg;
    }

    /**
     * 修改数据
     *
     * @param testOptions 实例对象
     * @return 实例对象
     */
    @PutMapping(value = "/{id}", produces = {"application/json;charset=utf-8"})
    public ResponseEntity update(TestOptions testOptions) {
        ResponseEntity resultMsg = new ResponseEntity(401, "修改失败", null);
        int row = this.testOptionsService.update(testOptions);
        if (row != 0) {
            resultMsg.setCode(200);
            resultMsg.setMessage("修改成功!");
        }
        return resultMsg;
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 主键数组
     * @return 是否成功
     */
    @DeleteMapping(value = "/{deleteids}",produces = {"application/json;charset=utf-8"})
    public ResponseEntity deleteById(@PathVariable("deleteids") int[] ids) {
        ResponseEntity resultMsg = new ResponseEntity(401, "删除成功!", null);
        for (int id :ids) {
            int row =  this.testOptionsService.deleteById(id);
            if (row == 0) {
                resultMsg.setCode(201);
                resultMsg.setMessage("删除失败!");
                return resultMsg;
            }
        }
        return resultMsg;
    }

}
